package com.example.demo.controller;

import com.example.demo.entity.Notation;
import com.example.demo.entity.TheoryKnowledge;
import com.example.demo.service.TheoryKnowledgeService;
import com.example.demo.util.FileUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("v1")
public class TheoryKnowledgeController {

    @Autowired
    private TheoryKnowledgeService theoryKnowledgeService;

    //分页查询所有理论知识信息
    @GetMapping("/tkpage")
    public PageInfo<TheoryKnowledge> getPagesTheoryKnowledge(Integer pageNum, Integer pageSize){
        PageInfo<TheoryKnowledge> pageInfo = theoryKnowledgeService.findTheoryKnowledgePage(pageNum,pageSize);
        return pageInfo;
    }

    //查询所有理论知识信息
    @GetMapping("/tk1")
    public List<TheoryKnowledge> getTheoryKnowledge() {
        return theoryKnowledgeService.findTheoryKnowledgeAll();
    }

    //查询某些理论知识信息
    @GetMapping("/tk2/{theory_knowledge_id}")
    public TheoryKnowledge getTheoryKnowledgeById(@PathVariable Integer theory_knowledge_id) {
        return theoryKnowledgeService.findTheoryKnowledgeById(theory_knowledge_id);
    }

    //添加理论知识信息
    @PostMapping("/tk3")
    public Integer addTheoryKnowledgeInfo(@RequestBody TheoryKnowledge theoryKnowledge) {
        return theoryKnowledgeService.insertTheoryKnowledge(theoryKnowledge);
    }

    //修改理论知识信息
    @PutMapping("/tk4")
    public Integer updateTheoryKnowledgeInfo(@RequestBody TheoryKnowledge theoryKnowledge) {
        return theoryKnowledgeService.updateTheoryKnowledge(theoryKnowledge);
    }

    //删除理论知识信息
    @DeleteMapping("/tk5/{theory_knowledge_id}")
    public Integer deleteTheoryKnowledge(@PathVariable Integer theory_knowledge_id) {
        return theoryKnowledgeService.deleteTheoryKnowledge(theory_knowledge_id);
    }


    @PostMapping("/tk/upload")
    public String getPicFileName(MultipartFile file){
        String oldFileName = file.getOriginalFilename();
        System.out.println(oldFileName);
        String typeName = oldFileName.substring(oldFileName.lastIndexOf('.'));

        String filePath = FileUtil.getUploadFilePath();
        System.out.println(typeName);
        String newFileName = System.currentTimeMillis()+typeName;

        try {
            FileUtil.uploadFile(file.getBytes(),filePath,newFileName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return newFileName;
    }

    @GetMapping("/tk/hot")
    public List<TheoryKnowledge> getHotKnowledges() {
        return theoryKnowledgeService.getHotKnowledges();
    }

}